package com.imooc.o2o.service.impl;

import com.imooc.o2o.dao.ShopDao;
import com.imooc.o2o.dto.ShopExecution;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.enums.ShopStateEnum;
import com.imooc.o2o.exception.ShopOperationException;
import com.imooc.o2o.service.ShopServce;
import com.imooc.o2o.util.ImageUtil;
import com.imooc.o2o.util.PageCalcultaor;
import com.imooc.o2o.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author 徐老板
 * @date 2020/1/4   19:02
 */
@Service
@Transactional
public class ShopServceImpl implements ShopServce {

    @Autowired
    private ShopDao shopDao;

    @Override
    public ShopExecution addShop(Shop shop, InputStream shopImgInputStream,String fileName) {

/**
 *   首先对于引用类型参数要进行非空校验
 *   此处还应该对shopexecution中的两个实体类进行非空校验
 *   这里由于时间关系就不校验了
 *
 */

        if (shop==null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }

        try {
            /**
             * 首先设定前端没有而数据库中存在的初始值
             *
             */
        shop.setEnableStatus(0);
        shop.setCreateTime(new Date());
        shop.setLastEditTime(new Date());
        //加下来执行添加操作
            int effectedNum = shopDao.insertShop(shop);
            if (effectedNum <=0){
                 throw new ShopOperationException("创建店铺失败");
            }else {
                //接下来对传入的另一个参数做操作
                //插入图片
                if(shopImgInputStream!=null){
                    try {
                        addshopImgInputStream(shop,shopImgInputStream,fileName);
                    } catch (ShopOperationException e) { throw new ShopOperationException("添加店铺失败："+e.getMessage()); }
                    effectedNum = shopDao.updateShop(shop);
                    if (effectedNum <=0){
                        throw new ShopOperationException("更新图片地址失败");
                    }
                }
            }
        }catch (ShopOperationException e){
            throw new ShopOperationException("添加店铺失败："+e.getMessage());
        }


        return new ShopExecution(ShopStateEnum.CHECK,shop);
    }

    @Override
    public Shop getByShopId(int shopId) {
        return shopDao.queryShop(shopId);
    }

    @Override
    public ShopExecution mpdifyShop(Shop shop, InputStream shopImgInputStream, String fileName) {
        if (shop == null || shop.getShopId() == null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }else {
            //图片是否需要处理
            try {
                if (shopImgInputStream != null && fileName != null&& !"".equals(fileName)){
                    Shop oldShop = shopDao.queryShop(shop.getShopId());

                    if (oldShop != null) {
                        ImageUtil.deleteFileOrPath(oldShop.getShopImg());
                    }
                    addshopImgInputStream(shop,shopImgInputStream ,fileName );
                }
                //更新店铺信息
                shop.setLastEditTime(new Date());
                int num = shopDao.updateShop(shop);
                if (num !=1){
                    return new ShopExecution(ShopStateEnum.INNER_ERROE);
                }else {
                    shop = shopDao.queryShop(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS,shop);
                }
            } catch (Exception e) {
                throw new ShopOperationException("modifyShop error:" + e.getMessage());
            }
        }
    }

    @Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        int rowIndex = PageCalcultaor.calculateRowerIndex(pageIndex,pageSize );
        List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
        ShopExecution se = new ShopExecution();
        int count = shopDao.queryShopCount(shopCondition);

        if (shopList!=null){
            se.setShopList(shopList);
            se.setCount(count);
        }else {
            se.setState(ShopStateEnum.INNER_ERROE.getState());
        }

        return se;
    }

    private void addshopImgInputStream(Shop shop, InputStream shopImgInputStream,String fileName) {
        //获取图片目录的相对值路径
        String relativePath = PathUtil.getShopImagePath(shop.getShopId());
        String realPath = ImageUtil.generatethumbnailInputStream(shopImgInputStream,fileName,relativePath );
        System.out.println(realPath);
        shop.setShopImg(realPath);
    }


}
